package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.presenter.MineForwardPwdActivityPresenter;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;


@Route(path = RConfig.MINE_FORWARD_PWD)
public class MineForwardPwdActivity extends BaseActivity<MineForwardPwdActivityPresenter> {

    @BindView(R2.id.left_img)
    ImageView leftImg;
    @BindView(R2.id.login_account)
    EditText loginAccount;
    @BindView(R2.id.reset_code)
    EditText resetCode;
    @BindView(R2.id.get_code)
    TextView getCode;
    @BindView(R2.id.forward_pwd)
    EditText forwardPwd;
    @BindView(R2.id.pwd_two)
    EditText pwdTwo;
    @BindView(R2.id.register_btn)
    Button registerBtn;
    private Disposable timeDis;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_forward_pwd;
    }

    @Override
    public MineForwardPwdActivityPresenter getPresenter() {
        return new MineForwardPwdActivityPresenter();
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime()
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        getCode.setText(aLong + App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity1_j));
                    } else {
                        getCode.setText(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity2_j));
                        getCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void initView() {
        super.initView();
        ClickUtil.click(leftImg, () -> {
            finish();
        });
        ClickUtil.click(getCode, () -> {
            if (TextUtils.isEmpty(loginAccount.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity3_j));
                return;
            }
            mPresenter.getCode(loginAccount.getText().toString(), "2");
        });
        ClickUtil.click(registerBtn, () -> {  // 忘记密码
            String account = loginAccount.getText().toString();
            String code = resetCode.getText().toString();
            String pwd1 = forwardPwd.getText().toString();
            String pwd2 = pwdTwo.getText().toString();
            if (TextUtils.isEmpty(account)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity3_j));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineWithdrawActivity7_j));
                return;
            }
            if (TextUtils.isEmpty(pwd1) || !CheckUtil.isPayPwd(pwd1)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity5_j));
                return;
            }
            if (TextUtils.isEmpty(pwd2) || !CheckUtil.isPayPwd(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity6_j));
                return;
            }
            if (!pwd1.equals(pwd2)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineForwardPwdActivity7_j));
                return;
            }
            mPresenter.resetLoginPwd(account, pwd1, pwd2, code);
        });
    }

    public void sendCheckCodeFail() {
        getCode.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void resetPwdSuccess() {
        finish();
    }


}
